from tkinter import messagebox
import mysql.connector
from mysql.connector import Error

def connect_to_db(config):
    try:
        connection = mysql.connector.connect(**config)
        if connection.is_connected():
            print("数据库连接成功")
            return connection
    except Error as e:
        print(f"数据库连接失败: {str(e)}")
        messagebox.showerror("数据库错误", f"无法连接到数据库: {str(e)}")
        return None